import request from '@/router/axios';
import Mock from 'mockjs';

let _list = [
    {
        avatar:require('../assets/tou1.jpg'),  //头像
        id:1,  // id
        mail:'juhuixing@aliyun.com', //邮箱
        roleIds:2,  // 身份级别
        tel:'13488888888', // 手机号
        type:2, 
        username:'dtsmerchant',// 管理员名称
        password:'dtsmerchant' // 密码
    },
    {
        avatar:require('../assets/tou2.jpg'),
        id:2,
        mail:'juhuixing@aliyun.com',
        roleIds:1,
        tel:'13411111111',
        type:1,
        username:'yuyanshiman',
        password:'yuyanshiman'
    },
    {
        avatar:require('../assets/tou3.jpg'),
        id:3,
        mail:'1030809183@qq.com',
        roleIds:1,
        tel:'13466666666',
        type:2,
        username:'dtsadmin',
        password:'dtsadmin'
    },
    {
        avatar:require('../assets/tou4.jpg'),  //头像
        id:4,  // id
        mail:'juhuixing@aliyun.com', //邮箱
        roleIds:2,  // 身份级别
        tel:'13488888888', // 手机号
        type:2, 
        username:'dtsmerchant',// 管理员名称
        password:'dtsmerchant' // 密码
    },
    {
        avatar:require('../assets/tou5.jpg'),
        id:5,
        mail:'juhuixing@aliyun.com',
        roleIds:1,
        tel:'13411111111',
        type:1,
        username:'yuyanshiman',
        password:'yuyanshiman'
    },
    {
        avatar:require('../assets/tou6.jpg'),
        id:6,
        mail:'1030809183@qq.com',
        roleIds:1,
        tel:'13466666666',
        type:2,
        username:'dtsadmin',
        password:'dtsadmin'
    },
    {
        avatar:require('../assets/tou7.jpg'),  //头像
        id:7,  // id
        mail:'juhuixing@aliyun.com', //邮箱
        roleIds:2,  // 身份级别
        tel:'13488888888', // 手机号
        type:2, 
        username:'dtsmerchant',// 管理员名称
        password:'dtsmerchant' // 密码
    },
    {
        avatar:require('../assets/tou10.jpg'),
        id:8,
        mail:'juhuixing@aliyun.com',
        roleIds:1,
        tel:'13411111111',
        type:1,
        username:'yuyanshiman',
        password:'yuyanshiman'
    },
    {
        avatar:require('../assets/tou11.jpg'),
        id:9,
        mail:'1030809183@qq.com',
        roleIds:1,
        tel:'13466666666',
        type:2,
        username:'dtsadmin',
        password:'dtsadmin'
    },
    {
        avatar:require('../assets/tou12.jpg'),  //头像
        id:10,  // id
        mail:'juhuixing@aliyun.com', //邮箱
        roleIds:2,  // 身份级别
        tel:'13488888888', // 手机号
        type:2, 
        username:'dtsmerchant',// 管理员名称
        password:'dtsmerchant' // 密码
    },
    {
        avatar:require('../assets/tou2.jpg'),
        id:11,
        mail:'juhuixing@aliyun.com',
        roleIds:1,
        tel:'13411111111',
        type:1,
        username:'yuyanshiman',
        password:'yuyanshiman'
    },
    {
        avatar:require('../assets/tou3.jpg'),
        id:12,
        mail:'1030809183@qq.com',
        roleIds:1,
        tel:'13466666666',
        type:2,
        username:'dtsadmin',
        password:'dtsadmin'
    },
    {
        avatar:require('../assets/tou1.jpg'),  //头像
        id:13,  // id
        mail:'juhuixing@aliyun.com', //邮箱
        roleIds:2,  // 身份级别
        tel:'13488888888', // 手机号
        type:2, 
        username:'dtsmerchant',// 管理员名称
        password:'dtsmerchant' // 密码
    },
    {
        avatar:require('../assets/tou2.jpg'),
        id:14,
        mail:'juhuixing@aliyun.com',
        roleIds:1,
        tel:'13411111111',
        type:1,
        username:'yuyanshiman',
        password:'yuyanshiman'
    },
    {
        avatar:require('../assets/tou3.jpg'),
        id:15,
        mail:'1030809183@qq.com',
        roleIds:1,
        tel:'13466666666',
        type:2,
        username:'dtsadmin',
        password:'dtsadmin'
    },
]

let RoleList = [
    {
        label: "超级管理员",
        value: 1 
    },
    {
        label: "商户管理员",
        value: 2
    },
    {
        label: "推广管理员",
        value: 3
    },
    {
        label: "售货员",
        value: 4    
    }
]

//ids
// var ids = Mock.mock({"id|3-10":1000});

// 获取管理员列表
export const adminList = (data) =>request({
    url:'/adminList',
    method:'get',
    data
})
Mock.mock(/\/adminList/,config=>{
    let {pagesize,pagenum} = JSON.parse(config.body);
    //总条数
    let total = _list.length;
    //返回当前页数的对应的pagesize数量的数据
    if(pagesize && pagenum){
        let user = _list.filter((item,index)=>index<pagesize*pagenum && index>=pagesize*(pagenum-1));
        let arr = [];
        for(let item of user){
            let {avatar,id,mail,roleIds,tel,type,username} = item
            arr.push({avatar,id,mail,roleIds,tel,type,username})
        }
        return {
            data:{
                arr,total,pagenum,pagesize
            },
            meta:{
                msg:'请求成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'获取失败',
                status:400
            }
        }
    }
})

// 获取权限列表
export const roleList = () =>request({
    url:'/roleList',
    method:'get'
})
Mock.mock(/\/roleList/,config=>{
    return {
        data:{
            RoleList
        },
        meta:{
            msg:'请求成功',
            status:200
        }
    }
})


//添加管理员
export const addAdmin = (data) =>request({
    url:'/addAdmin',
    mothed:'post',
    data
});
Mock.mock(/\/addAdmin/,config=>{
    var ids = Mock.mock({"id|15-30":1000});
    let datas = JSON.parse(config.body)
    let {roleIds,username,password} = datas;
    if(roleIds && username && password){
        _list.unshift({
            avatar:datas.avatar?datas.avatar:require('../assets/toumr.jpg'),
            id:ids.id,
            mail:datas.mail?datas.mail:'',
            roleIds:roleIds,
            tel:datas.tel?datas.tel:'',
            type:2,
            username,
            password,
        })
        return {
            meta:{
                msg:'添加成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'添加失败',
                status:400
            }
        }
    }
    
})

//删除管理员
export const detAdmin = (data) =>request({
    url:'/detAdmin',
    mothed:'delete',
    data
})
Mock.mock(/\/detAdmin/,config=>{
    let {id} = JSON.parse(config.body);
    if(id){
        for(let index in _list){
            if(_list[index].id==id){
                _list.splice(index,1)
            }
        }
        return {
            meta:{
                msg:'删除成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'删除失败',
                status:400
            }
        }
    }
    
})

//根据名称 查询管理员
export const queryAdmin = (data) =>request({
    url:'/queryAdmin',
    mothed:'get',
    data
})
Mock.mock(/\/queryAdmin/,config=>{
    let {username} = JSON.parse(config.body);
    let arr = [];
    if(username){
        arr = _list.filter((item,index)=>item.username.indexOf(username)!=-1);
        let total = arr.length;
        let pagesize = arr.length
        return {
            data:{
                arr,total,pagesize
            },
            meta:{
                msg:'获取成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'参数错误',
                status:400
            }
        }
    }
})

// 修改管理员
export const editAdmin = (data) =>request({
    url:'/editAdmin',
    mothed:'put',
    data
})
Mock.mock(/\/editAdmin/,config=>{
    let datas = JSON.parse(config.body);
    let {id} = datas;
    if(id){
        for(let item of _list){
            if(item.id==id){
                item.avatar=datas.avatar?datas.avatar:item.avatar;
                item.mail=datas.mail?datas.mail:item.mail;
                item.roleIds=datas.roleIds?datas.roleIds:item.roleIds;
                item.tel=datas.tel?datas.tel:item.tel;
                item.username=datas.username?datas.username:item.username;
            }
        }
        return {
            meta:{
                msg:'修改成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'参数错误',
                status:400
            }
        }
    }
   
})
